package com.bw.z.mvp.presenter;

import com.blankj.utilcode.util.LogUtils;
import com.bw.z.mvp.HttpCode;
import com.bw.z.mvp.base.presenter.BasePresenter;
import com.bw.z.mvp.contract.NewsContract;
import com.bw.z.mvp.model.entity.BaseEntity;
import com.bw.z.mvp.model.entity.NewsEntity;

import java.util.Map;

import javax.inject.Inject;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class NewsPresenter extends BasePresenter<NewsContract.NewsModel, NewsContract.NewView> {
    private int page = 1;
    private LoadMoreOb ob;

    @Inject
    public NewsPresenter(NewsContract.NewsModel newsModel, NewsContract.NewView newView) {
        super(newsModel, newView);
        ob = new LoadMoreOb(this);
    }

    //请求全部数据的方法
    public void requestAll(Map<String, Object>... maps) {
        //针对列表接口中的请求参数中的page进行二次拼接操作
        for (int i = 0; i < maps.length; i++) {
            int code = (int) maps[i].get("code");
            switch (code) {
                case HttpCode.CHANNELNEWSCODE:
                    maps[i].put("page", page);
                    break;
                case HttpCode.FOLLOWNEWSCODE:
                    maps[i].put("page", page);
                    break;
                case HttpCode.USERFOLLOWCODE:
                    maps[i].put("page", page);
                    break;
                case HttpCode.VIDEONEWSCODE:
                    maps[i].put("page", page);
                    break;
                case HttpCode.LIVECODE:
                    maps[i].put("page", page);
                    break;
                case HttpCode.SELCOMMENTCODE:
                    maps[i].put("page", page);
                    break;
                case HttpCode.ADDCOMMENTCODE:
                    maps[i].put("page", page);
                    break;
                case HttpCode.SELLIVECOMMENTCODE:
                    maps[i].put("page",page);
                    break;
            }
        }
        m.requestAll(maps)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);
    }

    public void requestRefresh(Map<String, Object> map) {
        page = 1;
        map.put("page", page);
        m.requestRefreshLoad(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(ob);
    }

    public void requestLoad(Map<String, Object> map) {
        page += 1;
        map.put("page", page);
        m.requestRefreshLoad(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(ob);
    }

    @Override
    public void onNext(@NonNull BaseEntity baseEntity) {
        LogUtils.json(baseEntity);
        //使用instanceof区分entity类型->列表数据->refreshRecyckerView
        //非列表数据->refreshAll
        if (baseEntity instanceof NewsEntity) {
            //新闻列表返回值
            v.refreshRecyclerView(baseEntity);
            return;
        }
        v.refreshAll(baseEntity);
    }

    public static class LoadMoreOb implements Observer<BaseEntity> {

        private NewsPresenter newsPresenter;

        public LoadMoreOb(NewsPresenter newsPresenter) {
            this.newsPresenter = newsPresenter;
        }

        @Override
        public void onSubscribe(@NonNull Disposable d) {
            newsPresenter.onSubscribe(d);
        }

        @Override
        public void onNext(@NonNull BaseEntity baseEntity) {
            newsPresenter.loadMoreNext(baseEntity);
        }

        @Override
        public void onError(@NonNull Throwable e) {
            newsPresenter.onError(e);
        }

        @Override
        public void onComplete() {
            newsPresenter.onComplete();
        }
    }

    public void loadMoreNext(BaseEntity entity) {
        v.loadMoreRecyclerView(entity);
    }


}
